Query Statistics PRO
The queryStatistics method retrieves aggregated statistics for a specific health quantity type over a defined date range. It can compute metrics such as total sum, average, minimum, maximum, most recent value, and duration, with optional support for breaking down results by source (e.g., device or app).
This method is ideal for producing daily, weekly, or historical health summaries.
Method Signature
Parameters
quantityType: HealthQuantityType (required)
The health quantity type to query, such as:
"stepCount""heartRate""bodyMass""activeEnergyBurned"- Any supported
HealthQuantityType
options (optional)
An object specifying filtering and configuration options for the query.
Available HealthStatisticsOptions
Return Value
Returns a Promise that resolves to a HealthStatistics object, or null if no data is available for the given type and range.
Use the returned HealthStatistics object to access computed values like:
sumQuantity(...)averageQuantity(...)mostRecentQuantity(...)duration(...)
Example: Query Daily Step Count Summary
Example: Query Average Heart Rate from This App Only
Notes
- If
statisticsOptionsis not specified, some fields (like sum, average, or most recent) may returnnull. - This method returns aggregated values—to access raw samples, use
queryQuantitySamples()instead. - Results depend on the type of quantity. For instance, heart rate supports
discreteAverage, while step count supportscumulativeSum.
